<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90">
      <el-form-item label="来源渠道" prop="channelCode">
        <el-select v-model="queryParams.channelCode" placeholder="请选择渠道" clearable>
          <el-option
            v-for="dict in channelList"
            :key="dict.channelCode"
            :label="dict.channelName"
            :value="dict.channelCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="platformUserId">
        <el-input
          v-model="queryParams.platformUserId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子卡号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入电子卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属公司" prop="companyId">
        <el-tree-select
          v-model="queryParams.companyId"
          :data="companyList"
          :render-after-expand="true"
          :props="treeProps"
          node-key="id"
          clearable check-strictly
          @change="companyChange"
        />
      </el-form-item>
      <el-form-item label="所属线路" prop="lineId">
        <el-select v-model="queryParams.lineId" placeholder="请选择所属线路" clearable :disabled="!queryParams.companyId">
          <el-option
            v-for="dict in lineList"
            :key="dict.id"
            :label="dict.lineName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNum">
        <el-input
          v-model="queryParams.plateNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付模式" prop="payMode">
        <el-select v-model="queryParams.payMode" placeholder="请选择支付模式" clearable>
          <el-option
            v-for="dict in plat_order_pay_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="foldUp" label="支付方式" prop="payChannel">
        <el-select v-model="queryParams.payChannel" placeholder="请选择支付方式" clearable>
          <el-option
            v-for="dict in plat_order_pay_channel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="foldUp" label="机具订单号" prop="machineOutTradeNo">
        <el-input
          v-model="queryParams.machineOutTradeNo"
          placeholder="请输入机具订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="foldUp" label="商户订单号" prop="platOrderNo">
        <el-input
          v-model="queryParams.platOrderNo"
          placeholder="请输入商户订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="foldUp" label="交易流水号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入交易流水号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="foldUp" label="扫码时间" prop="startTime">
        <el-date-picker clearable
          v-model="searchScanTime"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmScanDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="foldUp" label="扣款时间" prop="payTime">
        <el-date-picker clearable
          v-model="searchPayTime"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmPayDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button v-if="foldUp" icon="ArrowUp" @click="foldUp = !foldUp">折叠条件</el-button>
        <el-button v-else icon="ArrowDown" @click="foldUp = !foldUp">展开条件</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['bus:platOrderPre:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="platOrderPreList" @selection-change="handleSelectionChange">
        <!-- <el-table-column label="运营渠道" align="center" prop="businessChannel">
          <template #default="scope">
            <dict-tag :options="business_channel" :value="scope.row.businessChannel"/>
          </template>
        </el-table-column> -->
        <el-table-column label="来源渠道" align="center" prop="channelName" width="150" show-overflow-tooltip />
        <el-table-column label="用户编号" align="center" prop="platformUserId" width="150" show-overflow-tooltip />
        <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
        <el-table-column label="电子卡号" align="center" prop="cardNo" width="150" show-overflow-tooltip />
        <el-table-column label="公司名称" align="center" prop="subCompanyName" width="150" show-overflow-tooltip/>
        <el-table-column label="线路" align="center" prop="lineName" width="120" />
        <el-table-column label="车牌号" align="center" prop="plateNum" width="110" />
        <el-table-column label="票价（元）" align="center" prop="totalFee" width="100">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.totalFee) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="优惠金额（元）" align="center" prop="couponFee" width="120">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.couponFee) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="实扣金额（元）" align="center" prop="actualFee" width="120">
          <span>{{ '' }}</span>
        </el-table-column>
        <el-table-column label="支付模式" align="center" prop="payMode">
          <template #default="scope">
            <dict-tag :options="plat_order_pay_mode" :value="scope.row.payMode"/>
          </template>
        </el-table-column>
        <el-table-column label="支付方式" align="center" prop="payChannel" width="100">
          <template #default="scope">
            <dict-tag :options="plat_order_pay_channel" :value="scope.row.payChannel"/>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" align="center" prop="orderStatus" width="100">
          <template #default="scope">
            <dict-tag :options="plat_order_status" :value="scope.row.orderStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="扫码时间" align="center" prop="startTime" width="160" />
        <el-table-column label="扣款时间" align="center" prop="payTime" width="160" />
        <el-table-column label="交易流水号" align="center" prop="transactionId" width="150" show-overflow-tooltip />
        <el-table-column label="机具订单编号" align="center" prop="machineOutTradeNo" width="150" show-overflow-tooltip />
        <el-table-column label="商户订单号" align="center" prop="platOrderNo" width="150" show-overflow-tooltip />
        <el-table-column label="对账日期" align="center" prop="extFive" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.extFive, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        
        <!-- <el-table-column label="支付异常描述" align="center" prop="exDetail" /> -->
        <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="80">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="View" @click="showOrderDetail(scope.row)" v-hasPermi="['bus:platOrderPre:query']"></el-button>
            </el-tooltip>
            <template v-if="scope.row.orderStatus == 2">
              <el-tooltip content="重推" placement="top">
                <el-button link @click="rePushOrder(scope.row)" type="primary" icon="Setting" v-hasPermi="['bus:platOrderPre:edit']"></el-button>
              </el-tooltip>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="showDialog" :title="dialogTitle" width="70%">
      <order-detail 
        ref="orderDetail"
        @cancel="showDialog = false"
      />
    </Dialog>
  </div>
</template>

<script setup name="Abnormal">
import { listPlatOrderPre, pushOrder } from "@/api/order/ride"
import { listLine } from "@/api/bus/line"
import { listChannel } from "@/api/channel/platchannelinfo"
import { getAllCompanies } from '@/api/company/company'
import { nextTick } from "vue"
import OrderDetail from './detail.vue'

const orderDetail = ref()
const { proxy } = getCurrentInstance()
const { business_channel, plat_order_pay_mode, plat_order_pay_channel, plat_order_status } = proxy.useDict('business_channel', 'plat_order_pay_mode', 'plat_order_pay_channel', 'plat_order_status')

const platOrderPreList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const showDialog = ref(false)
const dialogTitle = ref('乘车订单详情')
const channelList = ref([])                 // 渠道列表
const companyList = ref([])                 // 公司列表
const subCompanyList = ref([])              // 分公司列表
const lineList = ref([])                    // 线路列表
const searchScanTime = ref([])
const searchPayTime = ref([])
const treeProps = {
  label: 'name',
  disabled: (data, node) => {
    return data.check == false
  }
}
const foldUp = ref(false)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    requestId: null,
    businessChannel: null,
    channelCode: null,
    // orderStatus: 2,
    platformUserId: null,
    mobile: null,
    cardNo: null,
    machineOutTradeNo: null,
    payMode: null,
    payChannel: null,
    companyId: null,
    subCompanyId: null,
    lineId: null,
    driverId: null,
    deviceId: null,
    plateNum: null,
    platOrderNo: null,
    transactionId: null,
    beginStartTime: null,
    endStartTime: null,
    beginPayTime: null,
    endPayTime: null,
    orderType: 1,       // 按照扫码时间倒序排列
    extTen: 3 //支付失败
  }
})

const { queryParams } = toRefs(data)

const confirmScanDate = (e) => {
  if (e && e.length) {
    queryParams.value.beginStartTime = e[0]
    queryParams.value.endStartTime = e[1]
  } else {
    queryParams.value.beginStartTime = null
    queryParams.value.endStartTime = null
  }
  handleQuery()
}

const confirmPayDate = (e) => {
  if (e && e.length) {
    queryParams.value.beginPayTime = e[0]
    queryParams.value.endPayTime = e[1]
  } else {
    queryParams.value.beginPayTime = null
    queryParams.value.endPayTime = null
  }
  handleQuery()
}

// 渠道列表
const getChannelList = () => {
  const params = {
    pageNum: 1,
    pageSize: 1000
  }
  listChannel(params).then(response => {
    channelList.value = response.rows
  })
}

/** 公司列表 */
function getCompanyList() {
  getAllCompanies().then(res => {
    companyList.value = res.data || []
  })
}

// 线路列表
const getLineList = () => {
  const params = {
    pageNum: 1,
    pageSize: 1000,
    companyId: queryParams.value.companyId
  }
  listLine(params).then(response => {
    lineList.value = response.rows
  })
}

/** 查询订单列表 */
function getList() {
  loading.value = true
  listPlatOrderPre(queryParams.value).then(response => {
    platOrderPreList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 选择所属公司
const companyChange = () => {
  queryParams.value.subCompanyId = ''
  queryParams.value.lineId = ''
  getLineList()
}

/** 订单详情 */
const showOrderDetail = (row) => {
  showDialog.value = true
  nextTick(() => {
    orderDetail.value.pageInit(row.id)
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 订单重推 */
function rePushOrder(row) {
  pushOrder(row.id).then(() => {
    proxy.$modal.msgSuccess("重推成功")
    getList()
  })
}

/** 重置按钮操作 */
function resetQuery() {
  searchScanTime.value = []
  searchPayTime.value = []
  queryParams.value.beginStartTime = null
  queryParams.value.endStartTime = null
  queryParams.value.beginPayTime = null
  queryParams.value.endPayTime = null
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bus/platOrderPre/export', {
    ...queryParams.value,
    exportType: 'ycdd'
  }, `异常订单_${proxy.getFileImportName()}.xlsx`)
}

getList()
getCompanyList()
getChannelList()
</script>

<style lang="scss" scoped>

</style>
